;$(function () {
    var id = $("#id").val();
    ids = [];
    var len = $("#userIds").val().length > 0;
    if(len){
        var temp =$("#userIds").val().split(",");
        for(var i = 0;i<temp.length;i++){
            ids.push(temp[i]);
        }
    }else {

        ids.push($("#userIds").val())
    }

    getInfo(id);
    contribute_validator();
    $("#selector").change(function(){
        selectorUser($(this).find('option:selected').val());
    });
});

var tale = new $.tale();//弹框

function contribute_validator() {
    $("#global-form").validator({
        fields: {
            title: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            startTimeStr: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            endTimeStr: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            site: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
        },
        invalid: function (form, errors) {
            return;
        },
        //验证成功
        valid: function (form) {
            saveInfo();
        },
    });
}


function getInfo(id){
    $.ajax({
        url: ctxPath+"/party/meeting/findById/"+id,
        type: 'GET',
        success: function(d){
            var success = d.success;
            if(success==true){
                var data=  d.data;
                umbinding.bind(data);
                ue.ready(function(){//监听编辑器实例化完成的事件
                    if(data.basicFunction!=null){
                        ue.setContent(data.basicFunction);
                    }
                });

            }
        }
    });
}

function saveInfo() {
    var remind = $("#remind").val();
    if (remind == '-1') {
        alert("请选择提醒");
        return;
    }
    var json =$("#global-form").serializeFormJSON();
    $.ajax({
        url: ctxPath+"/party/meeting/saveMeetingInfo",
        type: 'POST',
        data: json,
        success: function (d) {
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '会议保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = '/party/meeting/list';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
        }
    });
}

function choose(){
    $.ajax({
        url: ctxPath+"/agency/dept/getDepartment",
        type: 'get',
        success: function (d) {
            var success = d.success;
            if (success) {
                for(var i in d.data){
                    $("#selector").append("<option value='"+d.data[i].id+"'>"+d.data[i].name+"</option>");
                }

                $("#myModal").modal('show');
            } else {
                tale.alertError(result.message || '查询人员失败');
            }
        }
    });

}

function selectorUser(id){
    $("#list_result").html("");
    getList(id);
}

function getList(id){
    var deptUser = {deptId:id};
    var data = {pageNo:1,pageSize:15,deptUser:deptUser};
    $.ajax({
        url: ctxPath+"/agency/user/getUserPage",
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: function(d){
            if(d.data!=null){
                var html="";
                $.each(d.data.dataList,function(i,item){
                    html+=toHtml(item);
                })
                console.log(html);
                $("#list_result").html(html);
                getPage(d.data.pages,d.data.total);
            }

        }
    });
}

//分页
function getPage(pageCount,totalCount){
    kkpager.total =pageCount ;//总页码
    kkpager.totalRecords = totalCount;//总条数
    kkpager.generPageHtml({
        pno : 0,
        mode : 'click',//默认值是link，可选link或者click
        click : function(n){
            cur.constant.pageNo = n;
            getListPage();
            this.selectPage(n);
            return false;
        }
    });
}

function toHtml(data){
    var img='/image/defualt.png';
    if(data.fileId!=null){
        img=ctxPath+'/file/showImage/'+data.fileId;
    }
    var ext = data.userExtendEO;
    var dept = data.deptUser;
    var sex ="";
    var position ="";
    var deptName ="";
    var isParty ="";
    if(ext!=null){
        sex=ext.sex;
        position=ext.position;
        isParty=ext.isParty;
    }
    if(dept!=null){
        deptName=dept.deptName;
    }


    var checkboxer = '<input type="checkbox" id="userId'+data.id +'" value="'+data.id+'" onclick="checkUser(this)"/>';
    if(ids.indexOf(data.id.toString()) > -1){
        checkboxer = '<input type="checkbox" id="userId'+data.id +'" value="'+data.id+'" onclick="checkUser(this)" checked="checked"/>';
    }

// <a href="/agency/user/detail/'+data.id+'" style="cursor: pointer;">
    return '<tr>'
        +'<td>'
        +checkboxer
        +'</td>'
        +'<td>'
        +'<img src="'+img+'" width="50px;">&nbsp;&nbsp;&nbsp;&nbsp;'
        +'</td>'
        +'<td>'
        +data.nickname
        +'</td>'
        +'<td>'
        +sex
        +'</td>'
        +'<td>'
        +deptName
        +'</td>'
        +'<td>'
        +position
        +'</td>'
        +'<td>'
        +data.phone
        +'</td>'
        +'<td>'
        +isParty
        +'</td>'
        +'</tr>';
}

var ids = [];
function checkUser(obj){

    var $this = $(obj);
    if($this.prop("checked")){
        ids.push($this.val());
    }else {
        ids.remove($this.val());
    }

    $("#userIds").val(ids);

}
